package com.ruoyi.spspublic.controller;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.spspublic.domain.TMisMessage;
import com.ruoyi.spspublic.service.ITMisMessageService;
import com.ruoyi.spspublic.service.ITMisReadRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 我的待阅Controller
 *
 * @author wangwei
 * @date 2023-06-09
 */
@RestController
@RequestMapping("/spsMyInfo/message")
public class TMisMessageController extends BaseController {
  @Autowired private ITMisMessageService tMisMessageService;

  @Resource private ITMisReadRecordService readRecordService;

  /** 查询我的待阅列表 */
  //  @PreAuthorize("@ss.hasPermi('spsMyInfo:message:list')")
  @GetMapping("/list")
  public TableDataInfo list(TMisMessage tMisMessage) {
    startPage();
    List<TMisMessage> list = tMisMessageService.selectTMisMessageList(tMisMessage);
    // todo 先这么写 有空再改
    for (TMisMessage misMessage : list) {
      String tmp = misMessage.getCtBussId();
      misMessage.setCtBussId(misMessage.getCtId());
      misMessage.setCtId(tmp);
      //
      // misMessage.setCtType(ApplicationContextUtils.getBean(ISysDictDataService.class).selectDictLabel("message_task", misMessage.getCtType()));
      //      misMessage.setCtType(tMisMessage.getCtType());
    }
    return getDataTable(list);
  }

  /** 导出我的待阅列表 */
  @PreAuthorize("@ss.hasPermi('spsMyInfo:message:export')")
  @Log(title = "我的待阅", businessType = BusinessType.EXPORT)
  @GetMapping("/export")
  public AjaxResult export(TMisMessage tMisMessage) {
    List<TMisMessage> list = tMisMessageService.selectTMisMessageList(tMisMessage);
    ExcelUtil<TMisMessage> util = new ExcelUtil<TMisMessage>(TMisMessage.class);
    return util.exportExcel(list, "message");
  }

  /** 获取我的待阅详细信息 */
  //  @PreAuthorize("@ss.hasPermi('spsMyInfo:message:query')")
  @GetMapping(value = "/{ctId}")
  public AjaxResult getInfo(@PathVariable("ctId") String ctId) {
    return AjaxResult.success(tMisMessageService.selectTMisMessageById(ctId));
  }


  /** 修改我的待阅 */
  //  @PreAuthorize("@ss.hasPermi('spsMyInfo:message:edit')")
  @Log(title = "我的待阅", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
//  @SaveDataScope
  public AjaxResult edit(@RequestBody TMisMessage tMisMessage) {
    return toAjax(tMisMessageService.updateTMisMessage(tMisMessage));
  }

  /** 删除我的待阅 */
  //  @PreAuthorize("@ss.hasPermi('spsMyInfo:message:remove')")
  @Log(title = "我的待阅", businessType = BusinessType.DELETE)
  @PostMapping("/delete/{ctIds}")
  public AjaxResult remove(@PathVariable String[] ctIds) {
    return toAjax(tMisMessageService.deleteTMisMessageByIds(ctIds));
  }

  /**
   * 设置待阅为完成
   *
   * @param tMisMessage 请求体
   * @return 返回结果
   */
  //  @PreAuthorize("@ss.hasPermi('spsMyInfo:message:edit')")
  @Log(title = "我的待阅", businessType = BusinessType.UPDATE)
  @PostMapping(value = "/setFinish")
  public AjaxResult setFinish(@RequestBody TMisMessage tMisMessage) {
    return toAjax(tMisMessageService.setFinish(tMisMessage));
  }

  @GetMapping(value = "/tagList")
  public AjaxResult tagList() {
    return AjaxResult.success(tMisMessageService.tagList());
  }

  @GetMapping(value = "/getReadRecord")
  /*@ApiOperation(value = "todo待阅未知接口2", httpMethod = "GET")
  @ApiImplicitParams(
      value = {
        @ApiImplicitParam(
            value = "功能号",
            name = "functionNo",
            dataType = "String",
            paramType = "query"),
        @ApiImplicitParam(
            value = "单据id",
            name = "businessNo",
            dataType = "String",
            paramType = "query")
      })*/
  public AjaxResult getReadRecord(
      @RequestParam @NotNull String functionNo, @RequestParam @NotNull String businessNo) {
    return AjaxResult.success(readRecordService.getReadRecord(functionNo, businessNo));
  }
}
